package com.yanyi.security.auth.interceptor;


import com.yanyi.security.auth.configuration.ClientConfiguration;
import com.yanyi.security.auth.service.AuthClientService;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author: yukong
 * @date: 2018/7/25 18:30
 * @description:
 */
@Slf4j
public class ClientTokenInterceptor implements RequestInterceptor {
    @Autowired
    private ClientConfiguration clientConfiguration;
    @Autowired
    private AuthClientService authClientService;

    @Override
    public void apply(RequestTemplate requestTemplate) {

        try {
            requestTemplate.header(clientConfiguration.getClientTokenHeader(), authClientService.apply(clientConfiguration.getClientId(), clientConfiguration.getClientSecret()));
        } catch (Exception e) {
            log.error("set header error:{}",e.getMessage());
        }
    }
}